package cl.rizepe.playntest.core.views;

import static playn.core.PlayN.graphics;

import java.util.ArrayList;

import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.gl.GLContext;
import playn.core.gl.ImageLayerGL;
import pythagoras.f.Point;
import pythagoras.f.Transform;
import cl.rizepe.playntest.core.effects.Effect;
import cl.rizepe.playntest.core.effects.EffectFactory;

public class EffectedImageLayerGL extends ImageLayerGL implements EffectedLayer,UpdateableLayer {
	ArrayList<Effect> effects;

	public EffectedImageLayerGL (ImageLayer imageLayer){
		this(graphics().ctx(),imageLayer.image());
		EffectFactory.duplicateImageLayerAttributes(imageLayer,this);
	}

	public EffectedImageLayerGL(GLContext ctx) {
		super(ctx);
		effects = new ArrayList<Effect>();
	}

	public EffectedImageLayerGL(GLContext ctx, Image img) {
		super(ctx, img);
		effects = new ArrayList<Effect>();
	}

	public EffectedImageLayerGL clone(){
		EffectedImageLayerGL layerAdapter = new EffectedImageLayerGL(this.ctx,this.image());
		EffectFactory.duplicateImageLayerAttributes(this,layerAdapter);
		return layerAdapter;
	}

	public void updateEffects(){
		for (Effect effect: effects){  
			effect.refresh();
		}
	}

	public void addEffect(Effect effect){
		effects.add(effect);
	}

	public void removeEffect(Effect effect){
		if (effects.contains(effect))
			effects.remove(effect);
	}

	public float getScale(){
		return transform().uniformScale();
	}

	public void setCenter(Point point){
		setTranslation(point.x()-scaledWidth()/2, point.y()-scaledHeight()/2);
	}

	public Point getCenter(){		
		return new Point(transform().tx()+scaledWidth()/2-originX(),transform().ty()+scaledHeight()/2-originY());
	}

	public Point getTranslate(){
		Transform transform = transform();
		return new Point (transform.tx(),transform.ty());
	}

	public Point getSize(){
		return new Point(width(),height());
	}

	public Point getOrigin(){		
		return  new Point(originX(),originY());
	}

	public float getCircumscribedDiameter(){
		return (float) Math.sqrt(height()*height()+width()*width());
	}


	
	@Override
	public float getRotation() {
		return transform().rotation();
	}
	
	public void update(float delta){
	}

	@Override
	public void setTranslate(Point p) {
		setTranslation(p.x(),p.y());
		
	}
}




